package com.example.myapplication

import android.app.Application
import android.os.Build
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.FlutterBoost.BoostLifecycleListener
import com.idlefish.flutterboost.Utils
import com.idlefish.flutterboost.interfaces.INativeRouter
import io.flutter.app.FlutterApplication
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMessageCodec


class MyApplication : FlutterApplication() {
//    lateinit var flutterEngine : FlutterEngine

    override fun onCreate() {
        super.onCreate()
        val router =
            INativeRouter { context, url, urlParams, requestCode, exts ->
                val assembleUrl: String = Utils.assembleUrl(url, urlParams)
                PageRouter.openPageByUrl(context, assembleUrl, urlParams)
            }

        val boostLifecycleListener: BoostLifecycleListener = object : BoostLifecycleListener {
            override fun beforeCreateEngine() {}
            override fun onEngineCreated() {

//                // 注册MethodChannel，监听flutter侧的getPlatformVersion调用
//                val methodChannel = MethodChannel(
//                    FlutterBoost.instance().engineProvider().dartExecutor,
//                    "flutter_native_channel"
//                )
//                methodChannel.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
//                    if (call.method == "getPlatformVersion") {
//                        result.success(Build.VERSION.RELEASE)
//                    } else {
//                        result.notImplemented()
//                    }
//                }
//
//                // 注册PlatformView viewTypeId要和flutter中的viewType对应
//                FlutterBoost
//                    .instance()
//                    .engineProvider()
//                    .platformViewsController
//                    .registry
//                    .registerViewFactory(
//                        "plugins.test/view",
//                        TextPlatformViewFactory(StandardMessageCodec.INSTANCE)
//                    )
            }

            override fun onPluginsRegistered() {}
            override fun onEngineDestroy() {}
        }




        val platform = FlutterBoost.ConfigBuilder(this, router)
            .isDebug(true)
            .whenEngineStart(FlutterBoost.ConfigBuilder.ANY_ACTIVITY_CREATED)
            .renderMode(FlutterView.RenderMode.texture)
            .lifecycleListener(boostLifecycleListener)
            .build()
        FlutterBoost.instance().init(platform)

    }




}